﻿using System;
using System.Web.Security;
using LightWeb.Infrastructure.Repositories;
using System.Collections.Generic;
using LightWeb.DomainModel;
using System.Linq;

namespace LightWeb.WebCore.Security
{
    public class OrmRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            var rolesname = new List<string>();
            var roles = new DataContext().Read<Role>(null, null);
            foreach (var item in roles)
            {
                rolesname.Add(item.Name);
            }
            return rolesname.ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            List<string> roles = new List<string>();
            var context = DataContext.Current;
            var user = context.FindByProperty<User>("LoginName", username);
            if (user == null)
            {
                throw new Exception("User name was wrong");
            }
            foreach (var item in user.Roles)
            {
                roles.Add(item.RoleID.Name);
            }
            return roles.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var context = DataContext.Current;
            var role = context.FindByProperty<Role>("Name", roleName);
            List<string> users = new List<string>();
            if (role != null && role.Users.Count() > 0)
            {
                foreach (var item in role.Users)
                {
                    users.Add(item.UserID.LoginName);
                }
            }
            return users.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var context = DataContext.Current;
            var user = context.FindByProperty<User>("LoginName", username);
            var role = from p in user.Roles
                       where p.RoleID.Name.Equals(roleName)
                       select p;
            return role != null && role.Count() > 0;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            var context = DataContext.Current;
            var role = context.FindByProperty<Role>("Name", roleName);
            return role != null;
        }
    }
}
